package com.itic.system.user.attention.service;

import com.itic.appbase.framework.Base.BaseCode;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.system.user.user.persistence.model.HppUser;
import com.itic.system.user.user.service.HppUserService;
import com.itic.system.vo.AttentionVo;
import com.itic.system.vo.FanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.system.user.attention.persistence.dao.UserAttentionDao;
import com.itic.system.user.attention.persistence.model.UserAttention;
import com.itic.system.user.attention.persistence.model.UserAttentionExample;
import com.itic.system.user.attention.persistence.model.UserAttentionExample.Criteria;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class UserAttentionService
     extends CrudService<UserAttentionDao,UserAttention,UserAttentionExample> {
    @Autowired
    private UserAttentionDao userAttentionDao;
    @Autowired
    private HppUserService hppUserService;

    /**
     * 我关注的所有的用户
     *
     * @param userId
     * @return
     */
    public List<UserAttention> findMyAllAttention(String userId) {
        UserAttentionExample example = new UserAttentionExample();
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        criteria.andUserIdEqualTo(userId);
        List<UserAttention> userAttentions = this.list(example);
        return userAttentions;
    }

    /**
     * 我关注的列表
     *
     * @param example
     * @param userId
     * @return
     */
    public Map<String, Object> attentionList(UserAttentionExample example, String userId) {
        //效验userId用户是否存在
        Map<String, Object> resultMap = new HashMap<>();
        HppUser hppUser = this.hppUserService.get(userId);
        if (hppUser != null) {
            List<UserAttention> userAttentions = this.findAttentionListByPageAndUserId(example, userId);
            List<AttentionVo> attentionVos = new ArrayList<>();
            for (UserAttention userAttention : userAttentions) {
                AttentionVo attentionVo = this.userAttentionWrapToAttentionVo(userAttention);
                attentionVos.add(attentionVo);
            }
            resultMap.put(BaseCode.DATA, attentionVos);
            resultMap.put(BaseCode.CODE, BaseCode.SUCCESS_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SUCCESS_ERROR_MSG);
        } else {
            //提示用户存在
            resultMap.put(BaseCode.CODE, BaseCode.HPP_USER_NOT_EXSIT_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.HPP_USER_NOT_EXSIT_MSG);
        }
        return resultMap;
    }


    /**
     * 分页查询我关注的人
     *
     * @param example
     * @param userId
     * @return
     */
    public List<UserAttention> findAttentionListByPageAndUserId(UserAttentionExample example, String userId) {
        Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        List<UserAttention> userAttentions = this.list(example);
        return userAttentions;
    }

    /**
     * 查看我关注的人的数量
     *
     * @param userId
     * @return
     */
    public int findAttentionCountByUserId(String userId) {
        UserAttentionExample example = new UserAttentionExample();
        Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        int count = this.count(example);
        return count;
    }

    /**
     * 分页查询我粉丝的人
     *
     * @param example
     * @param userId
     * @return
     */
    public List<UserAttention> findFanListByPageAndUserId(UserAttentionExample example, String userId) {
        Criteria criteria = example.createCriteria();
        criteria.andAttentionUserIdEqualTo(userId);
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        List<UserAttention> userAttentions = this.list(example);
        return userAttentions;
    }

    /**
     * 查看粉丝数量
     *
     * @param userId
     * @return
     */
    public int findFanCountByUserId(String userId) {
        UserAttentionExample example = new UserAttentionExample();
        Criteria criteria = example.createCriteria();
        criteria.andAttentionUserIdEqualTo(userId);
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        int count = this.count(example);
        return count;
    }

    /**
     * userAttention 转换成AttentionVo
     *
     * @param userAttention
     * @return
     */
    public AttentionVo userAttentionWrapToAttentionVo(UserAttention userAttention) {
        AttentionVo attentionVo = new AttentionVo();
        attentionVo.setNickName(userAttention.getAttentionUserNickName());
        attentionVo.setHeadImage(userAttention.getAttentionUserImage());
        attentionVo.setUserId(userAttention.getAttentionUserId());
        return attentionVo;
    }

    /**
     * userAttention转换成FanVo
     *
     * @param userAttention
     * @return
     */
    public FanVo userAttentionWrapToFanVo(UserAttention userAttention) {
        FanVo fanVo = new FanVo();
        fanVo.setNickName(userAttention.getAttentionUserNickName());
        fanVo.setHeadImage(userAttention.getAttentionUserImage());
        fanVo.setUserId(userAttention.getAttentionUserId());
        fanVo.setBugCount(userAttention.getBuyCount());
        return fanVo;
    }

    /**
     * 我粉丝的列表
     *
     * @param example
     * @param userId
     * @return
     */
    public Map<String, Object> fanList(UserAttentionExample example, String userId) {
        //效验userId用户是否存在
        Map<String, Object> resultMap = new HashMap<>();
        HppUser hppUser = this.hppUserService.get(userId);
        if (hppUser != null) {
            List<UserAttention> userAttentions = this.findFanListByPageAndUserId(example, userId);
            List<FanVo> fanVos = new ArrayList<>();
            for (UserAttention userAttention : userAttentions) {
                FanVo fanVo = this.userAttentionWrapToFanVo(userAttention);
                fanVos.add(fanVo);
            }
            Map<String, Object> data = new HashMap<>();
            data.put("fanList", fanVos);
            data.put("nickName", hppUser.getNickName());
            resultMap.put(BaseCode.DATA, data);
            resultMap.put(BaseCode.CODE, BaseCode.SUCCESS_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SUCCESS_ERROR_MSG);
        } else {
            //提示用户存在
            resultMap.put(BaseCode.CODE, BaseCode.HPP_USER_NOT_EXSIT_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.HPP_USER_NOT_EXSIT_MSG);
        }
        return resultMap;
    }

    /**
     * 添加关注人
     *
     * @param attentionUserId 被关注人id
     * @param userId          关注人id
     * @return
     */
    public String addUserAttention(String attentionUserId, String userId) {
        String result = FAILED;
        HppUser attentionUser = this.hppUserService.get(attentionUserId);
        HppUser user = this.hppUserService.get(userId);
        if (attentionUser != null && user != null) {
            UserAttention userAttention = new UserAttention();
            userAttention.setAttentionUserId(attentionUser.getId());
            userAttention.setAttentionUserImage(attentionUser.getHeadImg());
            userAttention.setAttentionUserNickName(attentionUser.getNickName());
            userAttention.setBuyCount(1);
            userAttention.setUserHeadImage(user.getHeadImg());
            userAttention.setUserNickName(user.getNickName());
            userAttention.setUserId(user.getId());
            result = this.add(userAttention);
        }
        return result;
    }

    /**
     * 查询关注人
     *
     * @param attentionUserId 被关注人id
     * @param userId          关注人id
     * @return
     */
    public List<UserAttention> findUserAttentionByAttentionUserIdAndUserId(String attentionUserId, String userId) {
        UserAttentionExample example = new UserAttentionExample();
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        criteria.andAttentionUserIdEqualTo(attentionUserId);
        criteria.andUserIdEqualTo(userId);
        return this.list(example);
    }
}